/**
 * 动态加载js文件，不应用其他第三方库，使用原始的js
 * Created by long.hua on 2015/12/13.
 */

var STATIC_SOURCE_PREFIX ='./';

// 动态创建JS标签
var jsAppend = null;
function attachJs1(filename, appendId) {
    var tag = document.createElement('script');

    tag.type = 'text/javascript';
    tag.src = STATIC_SOURCE_PREFIX + filename;

    if(appendId != null && appendId != ''){
        jsAppend = document.getElementById(appendId);
    } else {
        jsAppend = document.getElementsByTagName("body")
        if(jsAppend != null && jsAppend != undefined){
            jsAppend = jsAppend[0];
        }
    }

    if(jsAppend != null){
        jsAppend.appendChild(tag);
    } else {
        console.log("attach js file appender is null!")
    }
}

function attachJs2(filename) {
    attachJs1(filename, null);
}

// 动态创建JS标签
function attachJs(filename, appendId){
    if(arguments.length == 1){
        attachJs2(filename);
    } else {
        attachJs1(filename, appendId);
    }
}


// 动态创建CSS标签
var cssAppend = null;
function attachCss1(filename, appendId) {
    var tag = document.createElement('link');

    tag.type = 'text/css';
    tag.rel = 'stylesheet';
    tag.href = STATIC_SOURCE_PREFIX + filename;

    if(appendId != null && appendId != ''){
        cssAppend = document.getElementById(appendId);
    } else {
        cssAppend = document.getElementsByTagName("head");
        if(cssAppend != null && cssAppend != undefined){
            cssAppend = cssAppend[0];
        }
    }

    if(cssAppend != null){
        cssAppend.appendChild(tag);
    } else {
        console.log("attach css file appender is null!")
    }
}

// 动态创建CSS标签
function attachCss2(filename) {
    attachCss1(filename, null);
}

// 动态创建CSS标签
function attachCss(filename, appendId) {
    if(arguments.length == 1){
        attachCss2(filename);
    } else {
        attachCss1(filename, appendId);
    }
}

// 动态创建IMG标签
function attachImg(filename, appendId, attrMap) {
    var tag = document.createElement('img');

    if(isArray(attrMap)){
        for(var i = 0; i < attrMap.length; i++){
            if(isArray(attrMap[i]) && attrMap[i].length == 2){
                var pair = attrMap[i];
                eval('tag.'+ pair[0] + ' = ' + pair[1] + ';');
            }
        }
    }
    tag.src = STATIC_SOURCE_PREFIX + filename;

    if(appendId != null && appendId != ''){
        var imgAppend = document.getElementById(appendId);
        if(imgAppend != null){
            imgAppend.appendChild(tag);
        } else {
            console.log("get image appender is null!")
        }
    } else {
        console.log("param appendId is null!")
    }
}

// 设置IMG标签来源
function setImgSrc(obj, filename) {
    obj.src = STATIC_SOURCE_PREFIX + filename;
}

// 判断是否为数组
function isArray(arr) {
    if(arr == undefined || arr == null){
        return false;
    }
    return Object.prototype.toString.call(arr) === '[object Array]';
}


// 动态创建JS标签
function loadJs(filename){
    document.write('<script src="' + STATIC_SOURCE_PREFIX + filename + '"></script>');
}

// 动态创建CSS标签
function loadCss(filename){
    document.write('<link rel="stylesheet" href="' + STATIC_SOURCE_PREFIX + filename + '" type="text/css" media="screen"/>');
}

// 动态创建IMG标签
function loadImg(filename, attrMap){
    if(!isArray(attrMap)){
        loadImg1(filename);
    } else {
        loadImg2(filename, attrMap);
    }
}

// 动态创建IMG标签
function loadImg1(filename){
    document.write("<img src='" + STATIC_SOURCE_PREFIX + filename + "'/>");
}

// 动态创建IMG标签
function loadImg2(filename, attrMap){
    var attr = '';
    if(isArray(attrMap)){
        for(var i = 0; i < attrMap.length; i++){
            if(isArray(attrMap[i]) && attrMap[i].length == 2){
                var pair = attrMap[i];
                if(attr == ''){
                    attr = pair[0] + '=' + pair[1];
                } else {
                    attr += ' ' + pair[0] + '=' + pair[1];
                }
            }
        }
    }

    document.write('<img ' + attr + ' src="' + STATIC_SOURCE_PREFIX + filename +'"/>');
}